package com.cloud.businesscomment.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloud.businesscomment.service.BusinessOrderService;
import com.cloud.businesscomment.service.BusinesscommentService;
import com.cloud.businesscomment.service.CreditRole;
import com.cloud.businesscomment.service.Level;
import com.cloud.commonapi.jsonResult.Result;
import com.cloud.commonapi.model.Business_comments;
import com.cloud.commonapi.model.Business_info;
import com.cloud.commonapi.model.Business_order;
import com.cloud.commonapi.model.User_info;
import com.netflix.client.http.HttpHeaders;
import com.netflix.ribbon.proxy.annotation.Http;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * author : 黄永佳
 * create : 2019/9/30 0030 16:08
 * modified :
 * 功能 :
 **/
@RestController
@ApiOperation(value = "业务订单评论模块")
@RequestMapping("/businesscomment")
public class BusinesscommentController {
    @Autowired
    BusinesscommentService businesscommentService;
    @Autowired
    BusinessOrderService businessOrderService;
    @Autowired
    RestTemplate restTemplate;

    @ApiOperation(value = "发布者评论订单")
    @PostMapping("/publisher")
    public Result addpublisher(Business_comments business_comments, HttpSession session){
//        session.setAttribute("loginId",1);
        Integer accountId=Integer.parseInt(session.getAttribute("loginId").toString());
        if(session.getAttribute("loginId")==null){
            return Result.build(400,"请登录");
        }
        Business_order business_order =businessOrderService.getorder(business_comments.getOrderId());
        if(business_order.getStatus()==2){
            business_order.setStatus(3);
        }else if(business_order.getStatus()==4){
            business_order.setStatus(5);
        }
        System.out.println(accountId);
        Integer receiverId=businesscommentService.findpublisherId(business_comments.getOrderId());
        business_comments.setReceiver(receiverId);
        business_comments.setPublisher(accountId);
        JSONObject jSONObject= restTemplate.getForObject("http://USERCENTER/usercontroller/userinfo/users/"+receiverId,JSONObject.class);
        JSONObject  data=jSONObject.getJSONObject("data");
        User_info user_info= JSON.parseObject(data.toString(), User_info.class);
        int star=Integer.parseInt(business_comments.getStar());
        if(star>=6){
            user_info.setCredit(CreditRole.addCredit(user_info.getCredit(),star));
        }else{
            user_info.setCredit(CreditRole.decreaseCredit(user_info.getCredit(),star));
        }
        System.out.println(user_info.getCredit());
        System.out.println(user_info.getAccountId());
        user_info.setLevel(Level.Role(user_info.getCredit()));
        MultiValueMap<String,String> param = new LinkedMultiValueMap<String,String>();
        param.add("accountId",accountId.toString());
        param.add("credit",user_info.getCredit().toString());
        param.add("level",user_info.getLevel());
        restTemplate.put("http://USERCENTER/usercontroller/userinfo/users/ByComment",param);
        business_comments.setStarttime(new Timestamp(new Date().getTime()));
        if(!businesscommentService.already(business_comments)){
            return Result.build(300,"您已评论过该订单");
        }
        if(businesscommentService.addpublisher(business_comments)>0){
            businessOrderService.updateorder(business_order);
            return Result.build(200,"评论成功");
        }else {
            return Result.build(300,"评论异常");
        }
    }
    @PostMapping("/recipient")
    @ApiOperation(value = "接单者评论订单")
    public Result addrecipient(Business_comments business_comments, HttpSession session) {
        if(session.getAttribute("loginId")==null){
            return Result.build(400,"请登录");
        }
        Business_order business_order =businessOrderService.getorder(business_comments.getOrderId());
        if(business_order.getStatus()==2){
            business_order.setStatus(4);
        }else if(business_order.getStatus()==3){
            business_order.setStatus(5);
        }
        Integer accountId=Integer.parseInt(session.getAttribute("loginId").toString());
        Integer receiverId=businesscommentService.findrecipientId(business_comments.getOrderId());
        business_comments.setReceiver(receiverId);
        business_comments.setPublisher(accountId);
        JSONObject jSONObject= restTemplate.getForObject("http://USERCENTER/usercontroller/userinfo/users/"+receiverId,JSONObject.class);
        JSONObject  data=jSONObject.getJSONObject("data");
        User_info user_info= JSON.parseObject(data.toString(), User_info.class);
        int star=Integer.parseInt(business_comments.getStar());
        if(star>=6){
            user_info.setCredit(CreditRole.addCredit(user_info.getCredit(),star));
        }else{
            user_info.setCredit(CreditRole.decreaseCredit(user_info.getCredit(),star));
        }
        user_info.setLevel(Level.Role(user_info.getCredit()));
        MultiValueMap<String,String> param = new LinkedMultiValueMap<String,String>();
        param.add("accountId",accountId.toString());
        param.add("credit",user_info.getCredit().toString());
        param.add("level",user_info.getLevel());
        restTemplate.put("http://USERCENTER/usercontroller/userinfo/users/ByComment",param);
        business_comments.setStarttime(new Timestamp(new Date().getTime()));

        if (!businesscommentService.already(business_comments)) {
            return Result.build(300, "您已评论过该订单");
        }
        if (businesscommentService.addpublisher(business_comments) > 0) {
            businessOrderService.updateorder(business_order);
            return Result.build(200, "评论成功");
        } else {
            return Result.build(300, "评论异常");
        }
    }
     @GetMapping("/{id}")
     @ApiOperation(value = "查看评论信息")
     public Result findByid(@PathVariable int id){
            HashMap<String,Business_comments> hashMap=new HashMap<>();
            List<Business_comments> business_comments=businesscommentService.findById(id);
            if(business_comments.size()>0){
//                hashMap.put("business_comments",business_comments);
                return Result.buildData(202,"查询成功",business_comments);
        }else {
                return Result.build(200,"该订单尚未评论");
            }
    }
}
